package com.fqcar.fqdf.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.baidu.location.BDLocation;
import com.fqcar.fqdf.DFApp;
import com.fqcar.fqdf.R;
import com.fqcar.fqdf.adapter.CarListAdapter;
import com.fqcar.fqdf.adapter.EndDealWithProblemsImageAdapter;
import com.fqcar.fqdf.adapter.EndDealWithProblemsReasonAdapter;
import com.fqcar.fqdf.adapter.ToDoCarListSelectTabAdapter;
import com.fqcar.fqdf.core.SwipLoadMoreActivity;
import com.fqcar.fqdf.entity.CarDetailEntity;
import com.fqcar.fqdf.service.RequestOptService;
import com.fqdf.framework.android.net.response.MidListRespBean;
import com.fqdf.framework.android.net.response.NetPageInfoBeanWrapper;
import com.fqdf.framework.android.util.Constants;
import com.fqdf.framework.android.view.LoadMoreListView;
import com.fqdf.framework.android.view.NoScrollGridLayoutManager;
import com.fqdf.framework.android.view.TwoSwipeRefreshLayout;
import com.fqdf.framework.android.view.listener.RecycleItemClickListener;
import com.fqdf.framework.android.view.recycleviewdivider.GridSpacingDecoration;
import com.fqdf.framework.android.view.recycleviewdivider.HorizontalLineSpacingDecoration;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;


public class ToDoCarListActivity extends SwipLoadMoreActivity implements RecycleItemClickListener{
    @BindView(R.id.no_data_layout)
    NestedScrollView noDataLayout;
    @BindView(R.id.list)
    LoadMoreListView list;
    @BindView(R.id.swiprefresh)
    TwoSwipeRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.main_back_iv)
    ImageView mainBackIv;
    @BindView(R.id.select_tab)
    RecyclerView selectTab;
    @BindView(R.id.no_data_iv)
    TextView noDataIv;

    private CarListAdapter adapter;
    private ToDoCarListSelectTabAdapter toDoCarListSelectTabAdapter;

    private MidListRespBean carRespBean;

    private BDLocation location;
    private String cause = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_todo_car_list);
        ButterKnife.bind(this);

        initData();
        initMyView();
    }

    private void initData() {
        location = ((DFApp) getApplication()).getCurLocation();
    }

    private void initMyView() {
        initRecycleView();
        adapter = new CarListAdapter(this);
        list.setAdapter(adapter);
        initSwipLoadMoreView(list, mSwipeRefreshLayout, noDataLayout);
        cause = (getResources().getStringArray(R.array.to_do_car_list_select_tab))[0];
        // 进行刷新
        refreshDataAnim();
    }
    /**
     * 初始化recycleview控件以及数据
     */
    private void initRecycleView() {

        NoScrollGridLayoutManager gridManager = new NoScrollGridLayoutManager(this, 3);
        selectTab.setLayoutManager(gridManager);

        toDoCarListSelectTabAdapter = new ToDoCarListSelectTabAdapter(this);
        selectTab.setAdapter(toDoCarListSelectTabAdapter);
        selectTab.addItemDecoration(new GridSpacingDecoration(getResources().getDimensionPixelOffset(R.dimen.dimen_15_dip),
                getResources().getDimensionPixelOffset(R.dimen.dimen_12_dip), false));

    }

    public void getCarList(int pageNum, String cause) {

        RequestOptService.getToDoCarListPageNum(getApplicationContext(), pageNum, cause, new Response.Listener<NetPageInfoBeanWrapper<CarDetailEntity>>() {
            @Override
            public void onResponse(NetPageInfoBeanWrapper<CarDetailEntity> response) {
                closeSwipRefreshAnim();
                if (handleResponse(response)) {
                    return;
                }
                carRespBean = response.getPageInfo();

                handleLoadMore(carRespBean);
            }
        }, getCloseSwipRefreshErrorListener());
    }


    @Override
    public void onRefresh() {
        getCarList(1, toDoCarListSelectTabAdapter.getReasonText());
    }

    @Override
    protected void loadMoreData() {
        getCarList(carRespBean.getPageNum() + 1, toDoCarListSelectTabAdapter.getReasonText());
    }

    @OnItemClick(R.id.list)
    public void onItemClick(int position) {
        CarDetailEntity carDetailEntity = adapter.getItem(position);
        Intent controlIntent = new Intent(ToDoCarListActivity.this, StartDealActivity.class);
        controlIntent.putExtra(Constants.BUNDLE_DATA, carDetailEntity.getCarId());
        startActivity(controlIntent);
    }

    @OnClick({R.id.main_back_iv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_back_iv:
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        adapter.clearList();
        getCarList(1,toDoCarListSelectTabAdapter.getReasonText());
    }

    @Override
    public boolean onItemLongClick(View view, int position) {
        return false;
    }
}
